from django.conf import settings
from django.db import models
from django.db.models.fields import Field
from django.utils.translation import gettext_lazy as _

############
## Fields ##
############

class PhoneNumberField(Field):
	"""
	Custom phone number field.
	"""
	def __init__(self, *args, **kwargs):
		super(PhoneNumberField, self).__init__(*args, **kwargs)
		self._include_prefix = kwargs.get('include_prefix', False)
		self._grouped = kwargs.get('grouped', True)

	def get_internal_type(self):
		return 'PhoneNumberField'

	def db_type(self):
		if settings.DATABASE_ENGINE == 'oracle':
			return 'CHAR(20)'
		else:
			return 'varchar(20)'

	def formfield(self, **kwargs):
		from localflavor.ng.forms import NGPhoneNumberField
		defaults = {
			'form_class': NGPhoneNumberField,
			'include_prefix': self._include_prefix,
			'grouped': self._grouped
		}
		defaults.update(kwargs)
		return super(PhoneNumberField, self).formfield(**defaults)

############
## Models ##
############

class State(models.Model):
	code = models.SlugField(max_length=3)
	name = models.CharField(max_length=30)

	class Meta:
		ordering = ('name',)
		verbose_name = _(u'Nigerian State')
		verbose_name_plural = _(u'Nigerian States')

	def __unicode__(self):
		return self.name

class LGA(models.Model):
	state = models.ForeignKey(State, related_name='local_govt_areas')
	name = models.CharField(max_length=30)

	class Meta:
		ordering = ('state', 'name',)
		verbose_name = _(u'Nigerian Local Government Area')
		verbose_name_plural = _(u'Nigerian Local Government Areas')

	def __unicode__(self):
		return self.name
